package cn.iocoder.yudao.module.wms.controller.admin.batch;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.wms.controller.admin.batch.vo.BatchPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.batch.vo.BatchRespVO;
import cn.iocoder.yudao.module.wms.controller.admin.batch.vo.BatchSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.batch.BatchDO;
import cn.iocoder.yudao.module.wms.service.batch.BatchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 批号设置")
@RestController
@RequestMapping("/wms/batch")
@Validated
public class BatchController {

    @Resource
    private BatchService batchService;

    @PostMapping("/create")
    @Operation(summary = "创建批号设置")
    @PreAuthorize("@ss.hasPermission('wms:batch:create')")
    public CommonResult<Long> createBatch(@Valid @RequestBody BatchSaveReqVO createReqVO) {
        return success(batchService.createBatch(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新批号设置")
    @PreAuthorize("@ss.hasPermission('wms:batch:update')")
    public CommonResult<Boolean> updateBatch(@Valid @RequestBody BatchSaveReqVO updateReqVO) {
        batchService.updateBatch(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除批号设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:batch:delete')")
    public CommonResult<Boolean> deleteBatch(@RequestParam("id") Long id) {
        batchService.deleteBatch(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得批号设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:batch:query')")
    public CommonResult<BatchRespVO> getBatch(@RequestParam("id") Long id) {
        BatchDO batch = batchService.getBatch(id);
        return success(BeanUtils.toBean(batch, BatchRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得批号设置分页")
    @PreAuthorize("@ss.hasPermission('wms:batch:query')")
    public CommonResult<PageResult<BatchRespVO>> getBatchPage(@Valid BatchPageReqVO pageReqVO) {
        PageResult<BatchDO> pageResult = batchService.getBatchPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BatchRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出批号设置 Excel")
    @PreAuthorize("@ss.hasPermission('wms:batch:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBatchExcel(@Valid BatchPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BatchDO> list = batchService.getBatchPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "批号设置.xls", "数据", BatchRespVO.class,
                        BeanUtils.toBean(list, BatchRespVO.class));
    }

}